package com.scott.ch24.configuration;

import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.*;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

/**
 * @author 黄后林 Spring容器不扫描Controller层
 * @version 1.0
 * @date 2021-11-25 09:14:12
 */
@Configuration
@EnableTransactionManagement
@ComponentScan(value = "com.scott.ch24",excludeFilters = {
        @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class})
})
public class SpringConfiguration {

    /**
     * 方法描述: 使用Druid连接数据库
     * @author: 黄后林
     * @date: 2021/11/25 9:33
     * @param
     * @return javax.sql.DataSource
     */
    @Bean
    public DataSource dataSource() throws PropertyVetoException {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/oa?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        return dataSource;
    }

    /**
     * 方法描述: 加载mapper文件
     * @author: 黄后林
     * @date: 2021/11/25 9:33
     * @param dataSource
     * @return org.mybatis.spring.SqlSessionFactoryBean
     */
    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) throws Exception{
        SqlSessionFactoryBean sqlSessionFactoryBean= new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath:dao/*Dao.xml");
        sqlSessionFactoryBean.setMapperLocations(resources);
        return  sqlSessionFactoryBean;
    }

    /**
     * 方法描述:加载dao接口所在地包
     * @author: 黄后林
     * @date: 2021/11/25 9:32
     * @param
     * @return org.mybatis.spring.mapper.MapperScannerConfigurer
     */
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() throws Exception{
        MapperScannerConfigurer mapperScannerConfigurer= new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.scott.ch24");
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        return mapperScannerConfigurer;
    }


    /**
     * 方法描述: 把事务管理器注册到容器中
     * @author: 黄后林
     * @date: 2021/11/24 14:11
     * @param
     * @return
     */
    @Bean
    public DataSourceTransactionManager transactionManager(DataSource dataSource) throws PropertyVetoException {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(dataSource);
        return  transactionManager;
    }
}
